//
//  FRPFullSizePhotoViewController.m
//  FunctionalReactivePixels
//
//  Created by Linyongzhi on 2017/4/6.
//
//

#import "FRPFullSizePhotoViewController.h"
#import "FRPPhotoViewController.h"

#import "FRPPhotoModel.h"
#import "FRPFullSizePhotoViewModel.h"

@interface FRPFullSizePhotoViewController () <UIPageViewControllerDataSource, UIPageViewControllerDelegate>

//@property (nonatomic, strong) NSArray *photoModelArray;

@property (nonatomic, strong) UIPageViewController *pageViewController;

@end

@implementation FRPFullSizePhotoViewController

- (instancetype)init {
    self = [super init];
    
    if (!self) {
        return nil;
    }
    
    self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:@{UIPageViewControllerOptionInterPageSpacingKey: @(30)}];
    self.pageViewController.dataSource = self;
    self.pageViewController.delegate = self;
    
    [self addChildViewController:self.pageViewController];

    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor blackColor];
    
    self.title = self.viewModel.initialPhotoName;
    
    NSArray *vcArray = @[[self photoViewControllerForIndex:self.viewModel.initialPhotoIndex]];
    [self.pageViewController setViewControllers:vcArray direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    
    self.pageViewController.view.frame = self.view.bounds;
    [self.view addSubview:self.pageViewController.view];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (FRPPhotoViewController*)photoViewControllerForIndex:(NSInteger)index {

    if (index >= 0 && index < self.viewModel.model.count) {
        FRPPhotoModel *photoModel = self.viewModel.model[index];
        FRPPhotoViewController *photoViewController =
        [[FRPPhotoViewController alloc] initWithNibName:@"FRPPhotoViewController" bundle:nil photoModel:photoModel index:index];
        return photoViewController;
    }
    // Index was out of bounds, return nil
    return nil;
}

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed {
    
    FRPPhotoViewController *firstVC = self.pageViewController.viewControllers.firstObject;
    
    self.title = [[firstVC photoModel] photoName];
    [self.delegate userDidScroll:self toPhotoAtIndex:firstVC.photoIndex];
}

- (UIViewController*)pageViewController:(UIPageViewController*)pageViewController viewControllerBeforeViewController:(FRPPhotoViewController *)viewController {
    return [self photoViewControllerForIndex:viewController.photoIndex - 1];
}

- (UIViewController*)pageViewController:(UIPageViewController*)pageViewController viewControllerAfterViewController:(FRPPhotoViewController *)viewController {
    return [self photoViewControllerForIndex:viewController.photoIndex + 1];
}

@end
